package org.xyz.common;

public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    public Result() {
        super();
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public static Result success(Object object) {
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg(ResultEnum.SUCCESS.getMsg());
        result.setData(object);
        return result;
    }

    /**
     * 成功但不带数据
     **/
    public static Result success() {

        return success(null);
    }

    /**
     * 失败
     **/
    public static Result error(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public enum ResultEnum {
        //这里是可以自己定义的，方便与前端交互即可
        UNKNOWN_ERROR(-1, "未知错误"),
        SUCCESS(2000, "成功"),
        USER_NOT_EXIST(1, "用户不存在"),
        USER_IS_EXISTS(2, "用户已存在"),
        TIME_IS_NULL(3, "次数已用完"),
        DAY_IS_NULL(4, "可用天数已为0"),
        TOKEN_NOT_EXIST(5, "兑换码无效"),
        TOKEN_HAS_USED(6, "兑换码已被使用"),
        PARAM_ERROR(7, "参数错误"),
        PASSWORD_ERROR(8, "密码错误"),
        ;
        private Integer code;
        private String msg;

        ResultEnum(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }
}
